<?php

namespace App\Service\User\v1_1_4;

use App\Helpers\Helper;
use App\Helpers\RedisUtil;
use App\Model\GiftLog;
use App\Model\Moment;
use App\Model\ProfileVerifyLog;
use App\Model\Setting\SettingAppHidden;
use App\Model\User;
use App\Model\UserCoin;
use App\Model\UserFreeCall;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Model\UserQa;
use App\Model\UserSetting;
use App\Model\UserSocialProfile;
use App\Model\UserTag;
use App\Service\BaseService;
use App\Model\UserChat;
/**
 * 用户详情页-其他信息
 */
class UserDetailExtraService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $profileUserId = $this->request->input('profile_user_id', 0);
        $profileUserId = $profileUserId ?: $userId;
        $profile = UserProfile::getProfile($profileUserId);
        $user = User::getUser($profileUserId);
        //个人介绍
        $ret['text_intro'] = $profile->self_intro ?: '';
        //动态相册
        $momentPics = Moment::getMomentPics($profileUserId, 4);
        $ret['moment_info'] = [
            'has_more' => 1,
            'list' => $momentPics ?: []
        ];
        //认证信息
        $personalInfo = UserProfilePersonal::getProfile($profileUserId);
        $wechatCheck = ProfileVerifyLog::checkVerified($profileUserId, ProfileVerifyLog::TYPE_WECHAT);
        $ret['verfy_info'] = [
            'list' => [
                ['verfied' => UserProfile::checkAvatarVrfied($profileUserId, $profile), 'text' => '真人认证', 'type' => 'avatar', 'show' => 1],
                ['verfied' => UserProfilePersonal::isFaceVerified($personalInfo), 'text' => '实名认证', 'type' => 'real', 'show' => 1],
                ['verfied' => $user->phone ? 1 : 0, 'text' => '手机认证', 'type' => 'phone', 'show' => 1],
                ['verfied' => $wechatCheck, 'text' => '微信认证', 'type' => 'wechat', 'show' => 0],
            ]
        ];
        //标签信息
        $tags = UserTag::getTagNames($profileUserId);
        $ret['tag_info'] = ['list' => $tags];

        //礼物信息
//        $giftList = GiftLog::getReceivedGiftInfo($profileUserId)['list'];
//        $ret['gift_info'] = [
//            'has_more' => 1,
//            'list' => $giftList,
//        ];
        //专访信息
        $qaList = UserQa::getDetailList($profileUserId);
        $ret['qa_info'] = [
            'list' => $qaList,
        ];
        //微信和搭讪
        $userProfile = UserProfile::getProfile($userId);
        // $userLevel = SettingUserLevel::getUserLevel($userProfile);
        $isHost = UserProfile::checkRoleHost($profile->role) ? 1 : 0;
        $ret['show_wechat'] = 0; //Helper::checkTempExcluded($userId, SettingAppHidden::FUNC_WECHAT) ? 0 : 1;
        $ret['show_dashan'] = $isHost;
        if($isHost == 1){
            $key = "chat_start_{$userId}_{$profileUserId}";
            $flag = RedisUtil::get($key);
            $ret['show_dashan'] = empty($flag) ? 1 : 0;
        }
        // log_user_result($userId, $ret);
        //弹窗
        $this->packPopInfo($ret, $userId, $userProfile);
        //主播设置显示
        $this->packHostSetting($ret, $profile);

        //是否有免费券
        $ret['free_call_coupon'] = (!$userProfile || $userProfile->gender != 1) ? 0
            : (UserFreeCall::checkValid(
                $userId,
                date('Y-m-d'),
                UserFreeCall::STATUS_OK,
                'count'
            ) ? 1 : 0);

        //喜欢的标签
        $tags = UserTag::getTagNames($profileUserId, UserTag::TYPE_LOVE);
        $ret['love_tag_info'] = ['list' => $tags];
        //微信账号
        $ret['fix_wechat_account'] = 1;
        check_dev_env() && log_user_result($userId, $ret);
        return $ret;
    }

    /**
     * 弹窗
     */
    function packPopInfo(&$ret = [], $userId, $selfProfile = null)
    {
        // if (!User::checkValidVersion($this->curVersion, $this->appId, [1 => '1.2.6'])) return;
        //完善资料
        $show = $this->checkProfilePop($selfProfile, $userId);
        $ret['pop_fixprofile'] = !$show ? null : [
            'show' => 1,
            'text' =>  '1、上传本人头像
2、设置声音介绍
3、发布照片墙照片/视频
4、发布爱情宣言',
            'title' => '完善资料',
            'sub_title' => '可获得更多异性青睐！',
            'button_txt' => '去完善资料',
        ];

        //免费券充值提醒
        if ($selfProfile->gender != 1) return;

        //已赠送的张数
        $freeCount = UserFreeCall::checkValid($userId, '', -2, 'count');
        if ($freeCount >= 1) {
            log_info(__CLASS__ . "=={$userId}==SendCount:{$freeCount}");
            return;
        }

        $userCoin = UserCoin::getUserInfo($userId);
        $coinLmt = 20;
        if ($userCoin['total_coin'] > $coinLmt) return;
        //显示弹窗间隔次数
        $showTimes = 3;
        $key = 'check_userdetail_' . $userId;
        if ($checkNum = RedisUtil::get($key)) RedisUtil::set($key, ++$checkNum, 3600 * 24);
        else RedisUtil::set($key, $checkNum = 1);
        $show = 1;
        //每隔3次显示弹窗
        if ($checkNum % $showTimes > 0) $show = 0;
        $ret['pop_recharge'] = [
            'show' => $show,
            'text' => '1分钟免费视频通话券',
            'title' => '收到福利',
            'button_txt' => '充值并收下福利'
        ];
    }

    /**
     * 是否显示完善资料弹窗
     */
    function checkProfilePop($profile = null, $userId = 0)
    {
        if (!$profile->voice || !$profile->poster || !$profile->self_intro) return 1;
        $socialProfile = UserSocialProfile::getProfile($userId, 'item');
        return $socialProfile ? 0 : 1;
    }

    /**
     * 主播设置显示
     */
    function packHostSetting(&$ret = [], $hostProfile = null)
    {
        // if (!User::checkValidVersion($this->curVersion, $this->appId, ['1.2.7'])) return;
        $setting = UserSetting::getShowPermit($hostProfile->user_id);
        //是否显示礼物墙
        $ret['show_gift'] = $setting['show_gift'];
        if (!$setting['show_gift']) $ret['gift_info'] = null;
    }
}
